package com.me.study.jdk8.date;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import static org.junit.Assert.fail;

/**
 * Java8：新 日期函数 测试类
 *
 * @author ME
 * @date 2020/10/24
 */
public class LocalDateTimeTest {

    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static LocalDateTime time1;
    private static LocalDateTime time2;
    private static LocalDateTime time3;

    /**
     * 每个 @Test 方法之前都会调用 @Before
     */
    @Before
    public void init() {
        time1 = LocalDateTime.of(2019, 12, 3, 8, 15, 0);
        time2 = LocalDateTime.of(2020, 1, 3, 19, 25, 30);
        time3 = LocalDateTime.of(2020, 1, 4, 0, 0);
    }

    @Test
    public void between() {
        long days = ChronoUnit.DAYS.between(time2.toLocalDate(), time3.toLocalDate());
        System.out.println("2020-01-03 19:25:30 距离 2020-01-02 相差几天（不足1天算1天）：" + days);

        days = ChronoUnit.DAYS.between(time3.toLocalDate(), time3.toLocalDate());
        System.out.println("2020-01-03 19:25:30 距离 2020-01-02 相差几天（不足1天算1天）：" + days);
    }

    @Test
    public void parse() {
        Assert.assertEquals("解析后时间相等 2019-12-03 08:15:00", time1, LocalDateTime.parse("2019-12-03 08:15:00", FORMATTER));

        System.out.println(LocalDateTime.parse("2020-09-12T00:00:00+08:00", DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    }

    @Test
    public void format() {
        Assert.assertEquals("格式化为 2019-12-03 08:15:00", "2019-12-03 08:15:00", FORMATTER.format(time1));
        Assert.assertEquals("格式化为 2020-01-03 19:25:30", "2020-01-03 19:25:30", FORMATTER.format(time2));
    }

    /** 判断日期的前后，不包含等于 */
    @Test
    public void before() {
        Assert.assertTrue("`2019-12-03 08:15:00` 早于 `2020-01-03 19:25:30`",
                time1.isBefore(time2));
        Assert.assertFalse("`2020-01-03 19:25:30` 不早于 `2020-01-03 19:25:30`",
                time2.isBefore(time2));
    }

    /** 日期加法运算 */
    @Test
    public void plus() {
        LocalDateTime dateTime = time1.plusYears(1) // 加1年
                .plusDays(2) // 加2天
                .plusSeconds(5);// 加5秒
        Assert.assertEquals("增加后的时间后本身是不变的：2019-12-03 08:15:00",
                FORMATTER.format(time1), "2019-12-03 08:15:00");
        Assert.assertEquals("增加后的新时间为：2020-12-05 08:15:05",
                FORMATTER.format(dateTime), "2020-12-05 08:15:05");
    }

    /** 日期减法运算 */
    @Test
    public void minus() {
        LocalDateTime dateTime = time1.minusYears(1) // 减 1年
                .minusDays(2) // 减 2天
                .minusSeconds(9);// 减 9秒
        Assert.assertEquals("减小后的时间后本身是不变的：2019-12-03 08:15:00",
                FORMATTER.format(time1), "2019-12-03 08:15:00");
        Assert.assertEquals("减小后的新时间为：2018-12-01 08:14:51",
                FORMATTER.format(dateTime), "2018-12-01 08:14:51");
    }

    /** 获取指定单位的数值，比如获取某时间的 分钟数 */
    @Test
    public void get() {
        Assert.assertEquals("`2020-01-03 19:25:30` 的年份是 `2020`",
                time2.getYear(), 2020);
        Assert.assertEquals("`2020-01-03 19:25:30` 的小时数是 `8`",
                time2.getHour(), 19);
        Assert.assertEquals("`2020-01-03 19:25:30` 的秒是 `30`",
                time2.getSecond(), 30);
    }

    /** 设置为指定时间 */
    @Test
    public void with() {
        Assert.assertEquals("time1 时间设置为：`2020-01-03 19:25:30`",
                FORMATTER.format(time1.with(time2)), "2020-01-03 19:25:30");
        Assert.assertEquals("time1 时间设置为：`2019-12-03 08:15:00`",
                FORMATTER.format(time1.withYear(1999)), "1999-12-03 08:15:00");
        Assert.assertEquals("time1 时间设置为：`2019-12-03 08:15:00`",
                FORMATTER.format(time1.withSecond(29)), "2019-12-03 08:15:29");
    }

}
